package com.phiture.erp.basic.web.controller.api;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.phiture.erp.basic.api.pojo.resp.ErpMaterialInfoResp;
import com.phiture.erp.basic.api.pojo.resp.ErpMaterialPlanResp;
import com.phiture.erp.basic.api.pojo.resp.ErpMaterialResp;
import com.phiture.erp.basic.api.service.ErpMaterialApi;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialInfoDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialPlanRespVO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialPlanService;
import com.phiture.erp.basic.core.service.ErpMaterialPurchasingService;
import com.phiture.erp.basic.core.service.ErpMaterialService;
import com.phiture.erp.basic.dal.entity.ErpMaterialDO;
import com.phiture.erp.basic.dal.entity.ErpMaterialPlanDO;
import com.phiture.erp.basic.dal.entity.ErpMaterialPurchasingDO;
import com.phiture.erp.basic.web.convert.ErpMaterialConvertMapper;
import com.phiture.erp.common.enums.ErpMaterialAttrEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

/**
 * @author YH
 * @date 2025/4/8
 */

@Service
@RequiredArgsConstructor
public class ErpMaterialApiServiceImpl implements ErpMaterialApi {
    private final ErpMaterialService erpMaterialService;
    private final ErpMaterialPlanService erpMaterialPlanService;
    private final ErpMaterialPurchasingService erpMaterialPurchasingService;

    @Override
    public List<ErpMaterialResp> validMaterialList(Collection<Long> materialIds) {
        List<ErpMaterialDO> list = erpMaterialService.validMaterilaList(materialIds);
        return ErpMaterialConvertMapper.INSTANCE.toErpMaterialRespList(list);
    }

    @Override
    public ErpMaterialResp getMaterial(Long materialId) {
        ErpMaterialDO materialDO = erpMaterialService.getMaterial(materialId);
        ErpMaterialResp result = ErpMaterialConvertMapper.INSTANCE.toErpMaterialResp(materialDO);
        ErpMaterialPlanDO materialPlanDO = erpMaterialPlanService.getMaterialPlanByMaterialId(materialId);
        if (materialPlanDO != null) {
            result.setPlanInfo(BeanUtils.toBean(materialPlanDO, ErpMaterialPlanResp.class, material -> {
                // 省略其他属性的赋值
            }));
        }
        return result;
    }

    @Override
    public List<ErpMaterialResp> getMaterialSimpleList() {
        List<ErpMaterialRespVO> list = erpMaterialService.getMaterialListByStatus(CommonStatusEnum.ENABLE.getStatus());
        // 上述查询结果List<ErpMaterialRespVO>转成List<ErpMaterialResp>
        return BeanUtils.toBean(list, ErpMaterialResp.class, material -> {
            // 省略其他属性的赋值
        });
    }

    @Override
    public List<ErpMaterialResp> getMaterialSelfMadeSimpleList() {
        List<ErpMaterialRespVO> list = erpMaterialService.getMaterialListByStatusAndAttr(CommonStatusEnum.ENABLE.getStatus(), ErpMaterialAttrEnum.SELF_MADE_PARTS.getAttr());
        // 上述查询结果List<ErpMaterialRespVO>转成List<ErpMaterialResp>
        return BeanUtils.toBean(list, ErpMaterialResp.class, material -> {
            // 省略其他属性的赋值
        });
    }



    @Override
    public Map<Long, ErpMaterialInfoResp> getMaterialMap(Collection<Long> ids) {
        List<ErpMaterialInfoDTO> materialList = erpMaterialService.getMaterialList(ids);
        Map<Long, ErpMaterialPurchasingDO> erpMaterialPurchasingMap = erpMaterialPurchasingService.getErpMaterialPurchasingMap(ids);
        Map<Long, ErpMaterialPlanRespVO> planRespVOMap = erpMaterialPlanService.getMaterialPlanRespVOMap(ids);
        List<ErpMaterialInfoResp> erpMaterialInfoRespList = ErpMaterialConvertMapper.INSTANCE.toErpMaterialInfoRespList(materialList);
        erpMaterialInfoRespList.forEach(item ->{
            ErpMaterialPlanRespVO respVO = planRespVOMap.get(item.getId());
            if(respVO != null){
                item.setRecevingLeadTime(respVO.getRecevingLeadTime());
            }
            ErpMaterialPurchasingDO purchasingDO = erpMaterialPurchasingMap.get(item.getId());
            if(purchasingDO != null){
                item.setSupplierId(purchasingDO.getSupplierId());
            }
        });
        return convertMap(erpMaterialInfoRespList, ErpMaterialInfoResp::getId);
    }
}
